package com.xin.libhtmlcontent.tv;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import com.xin.libhtmlcontent.R;
import com.xin.libhtmlcontent.tv.lib.DetailTagHandler;
import com.xin.libhtmlcontent.tv.lib.HtmlImageGetter;


public class TextViewHtmlContent extends androidx.appcompat.widget.AppCompatTextView {

    OnImgCallBack onImgCallBack;

    public TextViewHtmlContent(Context context) {
        super(context);
    }

    public TextViewHtmlContent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewHtmlContent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setHtmlContent(String html) {
        setMovementMethod(LinkMovementMethod.getInstance()); /*否则点击方法不执行*/
        Drawable drawable = getResources().getDrawable(R.drawable.default_img);
        Spanned sp = Html.fromHtml(html, new HtmlImageGetter(this, "/imgCache", drawable),
                new DetailTagHandler(getContext(), this));
        setText(sp);
    }

    public OnImgCallBack getOnImgCallBack() {
        return onImgCallBack;
    }

    public TextViewHtmlContent setOnImgCallBack(OnImgCallBack onImgCallBack) {
        this.onImgCallBack = onImgCallBack;
        return this;
    }
}
